﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cap.Services.Manager;
using Cap.Subscriber.Services;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace CAP.Webapi.Publisher.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class PublisherController : ControllerBase
    {

        private readonly ICapPublisher _publisher;
        private readonly ITestServices _testServices;

        public PublisherController(ICapPublisher publisher, ITestServices testServices)
        {
            //_logger = logger;
            _publisher = publisher;
            //_service = service;
            _testServices = testServices;
        }

        /// <summary>
        /// 普通方法，需要标注请求方式(无cap)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> Get()
        {
            return await _testServices.Query(1);
        }

        /// <summary>
        /// 控制器中使用cap（发布者订阅者都在控制器），需要标注路由，不需要标注请求方式
        /// </summary>
        /// <returns></returns>
        [Route("pub")]
        public ActionResult pub()
        {
            _publisher.Publish("cap.services.show.time", DateTime.Now);
            return Ok("发布成功!");
        }

        /// <summary>
        /// 控制器中的订阅者
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        [NonAction]
        [CapSubscribe("cap.services.show.time")]
        public ActionResult CheckReceivedMessage(DateTime time)
        {
            Console.WriteLine($"订阅成功:" + time);
            return Ok("订阅成功");
        }


        /// <summary>
        /// cap 中使用方法发布消息，不允许标注请求方式
        /// </summary>
        /// <returns></returns>
        [Route("test")]
        public async Task<ActionResult> test()
        {
            await _testServices.Insert();
            return Ok("ok!");
        }


        /// <summary>
        /// 凡是和cap发布相关的，都不允许标注请求方式，但是要请求该方法，只能标注路由
        /// </summary>
        /// <returns></returns>
        //[HttpGet] //不允许标注
        [Route("Transfer")]
        public async Task<ActionResult> Transfer()
        {
            await _testServices.Transfer();
            return Ok("Transfer!");
        }



    }
}
